package com.testfileupload.uploadimg.design.single;

/**
 * 双重检查
 */
public class SingleModel5 {
    public static void main(String[] args) {
        Single4 instence = Single4.getInstence();
        Single4 instence1 = Single4.getInstence();
        System.out.println(instence);
        System.out.println(instence1);
        System.out.println(instence.equals(instence1));
        System.out.println(instence == instence1);
    }
}

class Single4 {

    private static volatile Single4 single;

    /**
     * 构造私有化，不能new
     */
    private Single4() {
    }

    /**
     * 提供共有静态方法
     *
     * @return
     */
    public static Single4 getInstence() {
        if (single == null) {
            synchronized (Single4.class) {
                if (single == null) {
                    single = new Single4();
                }
            }
        }
        return single;
    }

}
